const app = getApp()
import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
      fileList: [],
      planAllocationCode: '',
      status: '',
      dateSting: '',
      timeString: '',
      imageCode: '',
      ton: '',
      lastInvalidValue: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      const { planAllocationCode = '', status = '' } = options
      console.log('planAllocationCode', planAllocationCode, 'status', status);
      this.setData({
        planAllocationCode,
        status
      })
    },

    onTonInput(event) {
      let value = event.detail.value;
      // 正则表达式：匹配正整数或最多两位小数的数字
      const regex = /^\d+(\.\d{0,2})?$/;
  
      if (regex.test(value)) {
        this.setData({
          ton: value,
          lastInvalidValue: '' // 重置无效值
        });
      } else {
        // 如果当前值与上一次的无效值不同，则提示用户
        if (this.data.lastInvalidValue !== value) {
          this.setData({
            lastInvalidValue: value // 更新无效值
          });
          // 提示用户输入不合法
          wx.showToast({
            title: '取消',
            icon: 'error'
          })
        }
      }
    },

    async afterRead(event) {
      const { file } = event.detail;
      const that = this
      const resCode = await requestUtil.request({url: configUrl.getFileOwnerCode, method: "GET"})
      // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
      wx.uploadFile({
        url: configUrl.fileUpload, // 接口地址
        filePath: file.url,
        header: requestUtil.createHeaders(),
        name: 'file',
        formData: {
            fileOwnerCode: resCode.data || '', // 文件编码
            fileGategory: 11, // 文件分类
            fileOwnerType: 5, // 文件所属类别 4 => 司机, 3 => 车辆
        },
        success(res) {
          // 上传完成需要更新 fileList
          const ret = JSON.parse(res.data)
          if (ret.data && ret.data.fileDownUrl) {
            const fileList = []
            const resUrl = ret.data.fileDownUrl;
            const imageCode = ret.data.fileOwnerCode;
            fileList.push({ ...file });
            that.setData({ poundUrl: resUrl, imageCode, fileList });
          }
 
        },
        fail: res => {
            wx.showToast({
                icon: 'error',
                title: '选取照片失败',
            })
          }
      });
    },
    // 选择日期
    bindChange(options) {
        console.log('bindChange',options.detail.value);
        this.setData({
            dateSting: options.detail.value,
        })
    },
    // 选择时间
    bindChangeTime(options) {
        this.setData({
            timeString: options.detail.value,
        })
    },
    // 提交数据
    async handleSubmit() {
 
      const { planAllocationCode, dateSting, timeString, status, inputGrossData, poundUrl, imageCode } = this.data
      if(!inputGrossData.input_value) {
        app.showToast('请选输入装车重量');
        return
      }

      if(!poundUrl) {
        app.showToast('请上传磅单');
        return
      }
      let londTon ='', unloadTon = '', loadFileCode = '', loadTime = '', unloadTime = '', unloadFileCode = '', newStatus = 1;
      let str = inputGrossData.input_value.charAt(inputGrossData.input_value.length - 1)
      if(str === '.') {
        str = inputGrossData.input_value + '00'
      } else str = inputGrossData.input_value
      switch (status) {
          case '接收':
              loadFileCode = imageCode;
              loadTime = dateSting + ' ' + timeString + ':' + '00';
              londTon = str;
              newStatus = 1
              break;
          case '已抢单':
              loadFileCode = imageCode;
              loadTime = dateSting + ' ' + timeString + ':' + '00';
              londTon = str;
              newStatus = 1
              break;
          case '上传卸车磅单':
              unloadFileCode = imageCode;
              unloadTime = dateSting + ' ' + timeString + ':' + '00';
              unloadTon = str;
              newStatus = 0
              break;
      
          default:
              break;
      }
      const arg0 = {
        loadTon: londTon,
        unloadTon,
        planAllocationCode,
        loadFileCode,
        unloadFileCode,
        loadTime,
        unloadTime,
        status: newStatus
      }
      const res = await  requestUtil.request({
        url: configUrl.inPutLoad,
        method: 'POST',
        data: arg0,
      })

    wx.switchTab({
        url: '/pages/home/home',
      })
      
    },
    // 删除照片
    handleDeleteUrrl() {
        this.setData({
            poundUrl: '',
            fileList: []
        })
    }
})